package com.xxg.fileshand.activity

import android.os.Bundle
import com.blankj.utilcode.util.LogUtils
import com.rscja.deviceapi.entity.UHFTAGInfo
import com.xxg.fileshand.adapter.TagCheckAdapter
import com.xxg.fileshand.base.BaseVMActivity
import com.xxg.fileshand.base.NoViewModel
import com.xxg.fileshand.bean.TagBean
import com.xxg.fileshand.databinding.ActivityTagCheckBinding
import com.xxg.fileshand.reader.ReaderUtils

class TagCheckActivity : BaseVMActivity<ActivityTagCheckBinding, NoViewModel>() {

    var tagCheckAdapter: TagCheckAdapter? = null
    var list = mutableListOf<TagBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        tagCheckAdapter = TagCheckAdapter()
        mBinding.rvList.adapter = tagCheckAdapter
        tagCheckAdapter?.setNewInstance(list)
        showRightText("重读")

//        var timer = Timer()
//        timer.schedule(object : TimerTask() {
//            override fun run() {
//                runOnUiThread {
//
//                }
//            }
//        }, 100, 200)
    }

    override fun onEpcRead(tagInfo: UHFTAGInfo) {
        LogUtils.e("TID: ${tagInfo.tid} EPC: ${tagInfo.epc}")
        val tagBean = TagBean()
        tagBean.titleFirst = tagInfo.rssi.toString()
        tagBean.contentSecond = tagInfo.tid
        tagBean.contentThird = tagInfo.epc
        list.add(0, tagBean)
        tagCheckAdapter?.notifyDataSetChanged()
        mBinding.tvCount.text = "共读到标签：${list.size}个"
    }

    override fun getMyTitle(): String {
        return "标签测试"
    }

    override fun onLeftClick() {
        super.onLeftClick()
        onBackPressed()
    }

    override fun onRightClick() {
        if (ReaderUtils.instance.isScanningRFID.value == true) {
            toast("请先停止扫描")
            return
        }
        list.clear()
        tagCheckAdapter?.notifyDataSetChanged()
        ReaderUtils.instance.resetData()
        mBinding.tvCount.text = "共读到档案：0本"
    }

    override fun isScan(): Boolean {
        return true
    }
}